{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "L模式的像素值：=========================================================================================\n",
      "\n",
      "\n",
      "测试数据的维度： (28, 28, 1)\n",
      "模型概括:\n",
      "Model: \"sequential_1\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "conv2d_1 (Conv2D)            (None, 28, 28, 16)        160       \n",
      "_________________________________________________________________\n",
      "max_pooling2d_1 (MaxPooling2 (None, 14, 14, 16)        0         \n",
      "_________________________________________________________________\n",
      "conv2d_2 (Conv2D)            (None, 14, 14, 32)        4640      \n",
      "_________________________________________________________________\n",
      "max_pooling2d_2 (MaxPooling2 (None, 7, 7, 32)          0         \n",
      "_________________________________________________________________\n",
      "flatten_1 (Flatten)          (None, 1568)              0         \n",
      "_________________________________________________________________\n",
      "dropout_1 (Dropout)          (None, 1568)              0         \n",
      "_________________________________________________________________\n",
      "dense_1 (Dense)              (None, 128)               200832    \n",
      "_________________________________________________________________\n",
      "dropout_2 (Dropout)          (None, 128)               0         \n",
      "_________________________________________________________________\n",
      "dense_2 (Dense)              (None, 10)                1290      \n",
      "=================================================================\n",
      "Total params: 206,922\n",
      "Trainable params: 206,922\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n",
      "网络层数: 9\n",
      "网络每一层的名称： [<keras.layers.convolutional.Conv2D object at 0x7f90a4faed68>, <keras.layers.pooling.MaxPooling2D object at 0x7f90a4fae668>, <keras.layers.convolutional.Conv2D object at 0x7f90a4f587f0>, <keras.layers.pooling.MaxPooling2D object at 0x7f90a4f58fd0>, <keras.layers.core.Flatten object at 0x7f90a4f58f60>, <keras.layers.core.Dropout object at 0x7f90a4f7a748>, <keras.layers.core.Dense object at 0x7f90a4f7a0f0>, <keras.layers.core.Dropout object at 0x7f90a4f35080>, <keras.layers.core.Dense object at 0x7f90a4f35278>]\n"
     ]
    },
    {
     "data": {
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gCIKQBEFoOzMlZd8/NyZwe/pnrULVyAzUwCKKABFFIAiCkARBaCIAlBEASiiAJSdOXkJOLWTw+7pE5zMFIAiCkARBaCIAlBEASiiABRRAIooAOVun7y0C1eKOdcd1tWQ/C5JzBSAA0QBKKIAFFEAiigARRSAIgpAEQWgiAJQRAEoogAUUQCKKABFFIAiCkARBaCIAlBEASiiABRRAIooAEUUgCIKQBEFoIgCUEQBKKIAFFEAiigARRSAIgpAEQWgiAJQRAEoogAUUQCKKABFFIAiCkARBaCIAlBEASiiAJRjRz0ATm6tddRD4BxjpgAUUQCKKABFFIAiCkARBaCIAlBEAShOXjpHzMxRD4EzhJkCUEQBKKIAFFEAiigARRSAIgpAEQWgiAJQRAEoogAUUQCKKABFFIAiCkARBaCIAlBEASiiABRRAIooAEUUgCIKQBEFoIgCUEQBKKIAFFEAiigARRSAIgpAEQWgiAJQRAEox456AJzczBz1EDjHmCkARRSAIgpAEQWgiAJQRAEoogAUUQDKrLWOegzADjFTAIooAEUUgCIKQBEFoIgCUP4DJuuwwDQ8NcIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 720x720 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# -*- coding: utf-8 -*-\n",
    "\n",
    "from keras.preprocessing import image     #导入numpy、matplotlib、keras、cv2\n",
    "import numpy as np\n",
    "from keras.models import load_model\n",
    "import cv2\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "img_path = \"./4-1.png\"                                  #测试图片路径\n",
    "img = image.load_img(img_path,target_size=(28,28))      #kreas加载测试图片,并reshape\n",
    "img = img.convert('L')                                  #测试图片转为灰度值\n",
    "#为灰色图像，它的每个像素用8个bit表示，0表示黑，255表示白，其他数字表示不同的灰度。\n",
    "#在PIL中，从模式“RGB”转换为“L”模式是按照下面的公式转换的： L = R * 299/1000 + G * 587/1000+ B * 114/1000\n",
    "\n",
    "print(\"L模式的像素值：=========================================================================================\")\n",
    "'''\n",
    "for i in range(28):                     #按行写入图片像素到目的文件中\n",
    "    for j in range(28):\n",
    "        pixel_r = int(img.getpixel((j,i)))    #每一个像素为4Byte  int型\n",
    "        print(pixel_r,end=' ')\n",
    "    print(\"\\n\")\n",
    "print(\"\\n\")\n",
    "'''\n",
    "print(\"\\n\")\n",
    "x = image.img_to_array(img)             #图像数据转为数组\n",
    "print(\"测试数据的维度：\",x.shape)                          #打印图像的维度尺寸\n",
    "x = np.expand_dims(x, axis=0)           #图像周围补0\n",
    "x=x.astype('float32')/255.              #图像像素归一化\n",
    "#print(x)\n",
    " #加载模型\n",
    "model = load_model('./model1.h5')    \n",
    "# 1、模型概括打印\n",
    "print(\"模型概括:\")  \n",
    "model.summary()\n",
    "#获取网络层数\n",
    "print(\"网络层数:\",len(model.layers))\n",
    "#获取每一层的名称\n",
    "print(\"网络每一层的名称：\",model.layers)\n",
    "\n",
    "preds = model.predict(x)   #模型测试\n",
    "#print(preds)               #打印测试结果\n",
    "n_examples=1\n",
    "plt.figure(figsize=(10,10))\n",
    "for i in range (n_examples):\n",
    "    ax=plt.subplot(2,n_examples,i+1)\n",
    "    plt.imshow(x[i, :, :, 0],cmap='gray')\n",
    "    plt.title(\"Predicted:{}\".format(np.argmax(preds[i])))\n",
    "    plt.axis('off')    \n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
